/**
 * related to restTime.ui
 * 设置休息时间
 * @Author : chenglm
 * @Timestamp : 2016-07-14
 */
var nf = sm("do_Notification");
var page = sm("do_Page");
var app = sm("do_App");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");
var hashdata = mm("do_HashData");
var dateTabs = mm("do_ListData");
var do_SegmentView_date = ui("do_SegmentView_date");
do_SegmentView_date.bindItems(dateTabs);

var baseId;
var param = {};
var hoursStr = "";

//andriod返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text = "设置服务休息时间"
var labelNext = ui(addheader+".labelNext");
labelNext.text = "确定";
labelNext.visible = true;
var headerRight = ui(addheader+".headerRight");

var hours = [{"hour":"7:00","selected":0,"bgColor":"00000000","val":7},
             {"hour":"8:00","selected":0,"bgColor":"00000000","val":8},
             {"hour":"9:00","selected":0,"bgColor":"00000000","val":9},
             {"hour":"10:00","selected":0,"bgColor":"00000000","val":10},
             {"hour":"11:00","selected":0,"bgColor":"00000000","val":11},
             {"hour":"12:00","selected":0,"bgColor":"00000000","val":12},
             {"hour":"13:00","selected":0,"bgColor":"00000000","val":13},
             {"hour":"14:00","selected":0,"bgColor":"00000000","val":14},
             {"hour":"15:00","selected":0,"bgColor":"00000000","val":15},
             {"hour":"16:00","selected":0,"bgColor":"00000000","val":16},
             {"hour":"17:00","selected":0,"bgColor":"00000000","val":17},
             {"hour":"18:00","selected":0,"bgColor":"00000000","val":18},
             {"hour":"19:00","selected":0,"bgColor":"00000000","val":19},
             {"hour":"20:00","selected":0,"bgColor":"00000000","val":20},
             {"hour":"21:00","selected":0,"bgColor":"00000000","val":21}
             ]; 
var hourGrids = mm("do_ListData");
var do_GridView_hour = ui("do_GridView_hour");
hourGrids.addData(hours);
do_GridView_hour.bindItems(hourGrids);

page.on("loaded", function() {
	var data = page.getData();
	if (data.typeId == homemakingUtil.typeId.DailyCleaning) {
		labelTitle.text = "设置服务上班时间";
	}
	init(data.id);
});

function init(id) {
	baseId = id;
	param.baseId = id;
	qqHttp.post(homemakingUtil.url.time.findByDateAndType, {baseId:id}, function(data) {
		param.serviceDateStr = data.serviceDateStr;
		dateTabs.removeAll();
		dateTabs.addData(data.dateList);
		dateTabs.updateOne(0, {"date":data.dateList[0].date,"dateStr":data.dateList[0].dateStr,"border":"ff7e00FF,1,3","fontColor":"ff7e00FF"});
		do_SegmentView_date.refreshItems({});
		
		var data_temp = hourGrids.getRange(0);
		for (var i = 0; i < hours.length; i++) {
			if (hours[i].val <= data.nowHour) {
				hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
			}
		}
		if (data.restHourList) {
			var restHourList = data.restHourList;
			for (var i=0; i<restHourList.length; i++) {
				for (var j=0; j<hours.length; j++) {
					if (hours[j].hour == restHourList[i].serviceHourStr) {
						hourGrids.updateOne(j, {"hour":data_temp[j].hour,"selected":1,"bgColor":"00000000"});
						hoursStr += data_temp[j].hour + ";";
						break;
					}
				}
			}
		}
		if (data.orderHourList) {
			var orderHourList = data.orderHourList;
			for (var i=0; i<orderHourList.length; i++) {
				for (var j=0; j<hours.length; j++) {
					if (hours[j].hour == orderHourList[i].serviceHourStr) {
						hourGrids.updateOne(j, {"hour":data_temp[j].hour,"selected":0,"bgColor":"f2f3f6FF"});
						break;
					}
				}
			}
		}
		do_GridView_hour.refreshItems();
	});
}

do_SegmentView_date.on("indexChanged", function(index) {
	var data_temp = dateTabs.getRange(0);
	param.serviceDateStr = data_temp[index].date;
	hoursStr = '';
    for (var i = 0; i < dateTabs.getCount(); i++) {
        if (i == index) {
        	dateTabs.updateOne(i, {
                "date" : data_temp[i].date,
                "dateStr" : data_temp[i].dateStr,
                "border" : "ff7e00FF,1,3",
                "fontColor" : "ff7e00FF"
            });
        } else {
        	dateTabs.updateOne(i, {
            	"date" : data_temp[i].date,
            	"dateStr" : data_temp[i].dateStr,
                "border" : "f2f3f6FF,1,3",
                "fontColor" : "949eaaFF"
            });
        }
    }
    do_SegmentView_date.refreshItems();
    refreshHours(index,data_temp[index].date);
});

//订阅GridView的touch点击事件
do_GridView_hour.on("touch", function(data) {
	var data_temp = hourGrids.getRange(0);
	if (data_temp[data].bgColor == 'f2f3f6FF') {
		return;
	}
	if (data_temp[data].selected == 1) {
		hourGrids.updateOne(data, {"hour":data_temp[data].hour,"selected":0,"bgColor":"00000000"});
		hoursStr = hoursStr.replace(data_temp[data].hour,""); 
	} else {
		hourGrids.updateOne(data, {"hour":data_temp[data].hour,"selected":1,"bgColor":"00000000"});
		hoursStr += data_temp[data].hour + ";";
	}
	do_GridView_hour.refreshItems();
})

/* 刷新时间 */
function refreshHours(index,serviceDateStr){
	qqHttp.post(homemakingUtil.url.time.findByDateAndType, {baseId:baseId,serviceDateStr:serviceDateStr}, function(data) {
		hourGrids.removeAll();
		hourGrids.addData(hours);
		hoursStr = "";
		var data_temp = hourGrids.getRange(0);
		if (index == 0) {
			for (var i = 0; i < hours.length; i++) {
				if (hours[i].val <= data.nowHour) {
					hourGrids.updateOne(i, {"hour":data_temp[i].hour,"selected":0,"bgColor":"f2f3f6FF"});
				}
			}
		}
		if (data.restHourList) {
			var restHourList = data.restHourList;
			for (var i=0; i<restHourList.length; i++) {
				for (var j=0; j<hours.length; j++) {
					if (hours[j].hour == restHourList[i].serviceHourStr) {
						hourGrids.updateOne(j, {"hour":data_temp[j].hour,"selected":1,"bgColor":"00000000"});
						hoursStr += data_temp[j].hour + ";";
						break;
					}
				}
			}
		}
		if (data.orderHourList) {
			var orderHourList = data.orderHourList;
			for (var i=0; i<orderHourList.length; i++) {
				for (var j=0; j<hours.length; j++) {
					if (hours[j].hour == orderHourList[i].serviceHourStr) {
						hourGrids.updateOne(j, {"hour":data_temp[j].hour,"selected":0,"bgColor":"f2f3f6FF"});
						break;
					}
				}
			}
		}
		do_GridView_hour.refreshItems();
	});
}

//确定事件
headerRight.on("touch",function(){
	if (!param || !param.serviceDateStr || !hoursStr) {
		nf.toast("请选择时间");
		return;
	}
	param.seviceHourStrs = hoursStr;
	qqHttp.post(homemakingUtil.url.time.addRestTime, param, function(data) {
		var msg = "保存成功";
		if (data.msg) {
			msg = msg + " " + data.msg;
		}
		nf.alert(msg);
	});
});